﻿using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace SuperX.Common.Respository
{
    // 
    public class IPAddressConverter : JsonConverter
    {
        // 
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(IPAddress);
        }

        //
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            IPAddress ipaddress = (IPAddress)value;
            writer.WriteValue(ipaddress.ToString());
        }

        // 
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return IPAddress.Parse(Extensions.Value<string>(JToken.Load(reader)));
        }
    }
}
